@tool
extends  EditorPlugin

var camera_menu:MenuButton
#var editor_interface:EditorInterface
var target_camera_path:NodePath = NodePath("Camera3D")

var is_keyboard_control_enabled:bool = false 
var move_speed:float = 1
var editor_cam:Camera3D = null

func _enter_tree() -> void:
	camera_menu = MenuButton.new()
	camera_menu.text = "相机"
	camera_menu.tooltip_text = "控制相机"
	
	camera_menu.get_popup().add_item("对齐场景相机",0)
	camera_menu.get_popup().connect("id_pressed",Callable(self,"_on_menu_item_pressed"))
	
	camera_menu.get_popup().add_check_item("相机漫游",1)
	camera_menu.get_popup().set_item_checked(0,is_keyboard_control_enabled)
	print("准备添加")
	add_control_to_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU,camera_menu)
	print("添加完成")
	
	set_process(true)
	
func _exit_tree()->void:
	remove_control_from_container(EditorPlugin.CONTAINER_TOOLBAR,camera_menu)
	camera_menu.queue_free()
	set_process(false)
	

func _process(delta: float) -> void:
	if not is_keyboard_control_enabled:
		return 
		
	if not editor_cam:
		var viewport_3d = EditorInterface.get_editor_viewport_3d(0)
		editor_cam = _get_editor_camera3d(viewport_3d)
		if not editor_cam:
			return 
	var move_dir:Vector3 = Vector3.ZERO
	if Input.is_action_pressed("ui_up"):
		editor_cam.translate(Vector3(0,0,move_speed))
	if Input.is_action_pressed("ui_down"):
		editor_cam.translate(Vector3(0,0,-move_speed))
	if Input.is_action_pressed("ui_left"):
		editor_cam.translate(Vector3(move_speed,0,0))
	if Input.is_action_pressed("ui_right"):
		editor_cam.translate(Vector3(-move_speed,0,0))
			
func _find_toolbar_container(node:Node)->Control:
	if node.is_class("Control"):
		print("name:",node.name," class name:",node.get_class())
	if node.is_class("Control") and node.name == "toolbar":
		return node
	for child in node.get_children():
		var ret = _find_toolbar_container(child)
		if ret:
			return ret 
	return null 

func _on_menu_item_pressed(id:int)->void:
	match id:
		0:
			_align_camera()
		1:
			#is_keyboard_control_enabled = not is_keyboard_control_enabled
			#camera_menu.get_popup().set_item_checked(1,is_keyboard_control_enabled)
			
func _align_camera()->void:
	if not Engine.is_editor_hint():
		push_warning("must use under editor env")
	
	var viewport_3d = EditorInterface.get_editor_viewport_3d(0)
	if not viewport_3d:
		push_error("can not find  3d view")
		return 
	
	var editor_cam = _get_editor_camera3d(viewport_3d)
	if not editor_cam:
		push_error("can not found camera in view3d")
		return 
		
	var edited_scene = EditorInterface.get_edited_scene_root()	
	if not edited_scene:
		push_error("please open scene")
		return 
	
	var scene_cam:Camera3D = null
	for child in edited_scene.get_children():
		if child.is_class("Camera3D"):
			scene_cam = child
			break
	if not scene_cam:
		push_error("can not find scene camera")
		return 	
	
	#editor_cam.global_transform = scene_cam.global_transform
	scene_cam.global_transform = editor_cam.global_transform
	#scene_cam.fov = editor_cam.fov
	#scene_cam.near = editor_cam.near
	#scene_cam.far = editor_cam.far 
	#scene_cam.projection = editor_cam.projection
	#scene_cam.size = editor_cam.size 
	#edited_scene.resource_changed()
	#editor_interface.show_message("success align")
	
	
func _get_editor_camera3d(viewport:Node)->Camera3D:
	for child in viewport.get_children():	
		if child.is_class("Camera3D"):
			return child 
	return null

func _get_toolbar_recursive(node:Node,index:int)->void:
	var equal_prefix = " "
	for i in range(index * 3):
		equal_prefix = equal_prefix + "="
		
	print(equal_prefix + node.name, " class name:",node.get_class())
	for child in node.get_children():
		_get_toolbar_recursive(child,index+1)
	
